<?php
class MessageController extends BackendController
{

    public $active = "plugins-message";
    public $plug_version = "1.0";
    public $plug_title = "客户留言";
    public $plug_description = "客户留言";
    public $plug_controller = "message";

    public function actionIndex(){

        $count = $this->connection->createCommand("select count(*) from rk_plug_message")->queryScalar();
        $pager = new CPagination($count);
        $pager->pageSize = 10;
        $pager->currentPage = isset($_REQUEST['page'])?$_REQUEST['page']:1;

        $offset = $pager->getOffset();
        $limit = $pager->getLimit();
        $messages = $this->connection->createCommand("select * from rk_plug_message limit :offset,:limit")
            ->bindParam(":offset",$offset)
            ->bindParam(":limit",$limit)
            ->queryAll();
        $this->data['messages'] = $messages;
        $this->data['pager'] = $pager;

        $this->render("index",$this->data);
    }

    public function actionInstall(){

        $this->connection->createCommand("DROP TABLE IF EXISTS `rk_plug_message`")->execute();
        $this->connection->createCommand("CREATE TABLE `rk_plug_message` (
  `plug_message_id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(255) DEFAULT NULL,
  `lastname` varchar(255) DEFAULT NULL,
  `content` text,
  `email` varchar(255) DEFAULT NULL,
  `createtime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`plug_message_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8")->execute();
        $this->connection->createCommand("insert into rk_plug (plug_controller,plug_version,plug_key) values (:controller,:version,:key)")
            ->bindParam(":controller",$this->plug_controller)
            ->bindParam(":version",$this->plug_version)
            ->bindParam(":key",$this->active)
            ->execute();
        $parentMenuId = $this->connection->createCommand("SELECT menu_id FROM rk_menu WHERE menu_key = 'plugins'")->queryScalar();
        $this->connection->createCommand("INSERT INTO rk_menu ( menu_text, parent_id, url, menu_key ) VALUES (:menu_text,:parent_id,'index.php?r=plugins/message',:menu_key)")
            ->bindParam(":menu_text",$this->plug_title)
            ->bindParam(":parent_id",$parentMenuId)
            ->bindParam(":menu_key",$this->active)
            ->execute();
        echo json_encode(AppCode::$success);
    }

    public function actionUninstall(){
        $this->connection->createCommand("delete from rk_plug where plug_key = :plug_key")
            ->bindParam(":plug_key",$this->active)
            ->execute();
        $this->connection->createCommand("DROP TABLE IF EXISTS `rk_plug_service`")->execute();
        $this->connection->createCommand("delete from rk_menu where menu_key = :menu_key")
            ->bindParam(":menu_key",$this->active)
            ->execute();
        echo json_encode(AppCode::$success);
    }

}